#include "date.h"
#include <cassert>
namespace date
{

	bool classDate::checkLeapYear()
	{
		if (0 == year%4 && 0 != year%100 || 0 == year%400)
		{
			return true;
		}

		return false;
	}

	void classDate::makeValidDate()
	{
		int numberDays[MAX_MONTH] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

		int intMonth = (static_cast<int>(month));
		bool valid = false; 

		while(!valid)
		{
			valid = true;

			if (checkLeapYear())
			{
				numberDays[1] = 29;
			}
			else
			{
				numberDays[1] = 28;
			}

			if(MAX_SECOND <= second)
			{
				minute += second/MAX_SECOND;
				second = second%MAX_SECOND;
				valid = false;
			}
			if(MIN_SECOND > second)
			{
				minute += (0 == second/MAX_SECOND ? -1 : second/MAX_SECOND);
				second = (0 == second%MAX_SECOND ? 0 : MAX_SECOND + second%MAX_SECOND);
				valid = false;
			}

			if(MAX_MINUTE <= minute)
			{
				hour += minute/MAX_MINUTE;
				minute = minute%MAX_MINUTE;
				valid = false;
			}
			if(MIN_MINUTE > minute)
			{
				hour += (0 == minute/MAX_MINUTE? -1 : minute/MAX_MINUTE);
				minute = (0 == minute%MAX_MINUTE? 0 : MAX_MINUTE + minute%MAX_MINUTE);
				valid = false;
			}

			if(MAX_HOUR <= hour)
			{
				day += hour/MAX_HOUR;
				hour = hour%MAX_HOUR;
				valid = false;
			}
			if(MIN_HOUR > hour)
			{
				day += (0 == hour/MAX_HOUR ? -1 : hour/MAX_HOUR);
				hour = (0 == hour%MAX_HOUR ? 0 : MAX_HOUR + hour%MAX_HOUR);
				valid = false;
			}

			if(MAX_MONTH < intMonth)
			{
				year = year + intMonth/MAX_MONTH;
				intMonth = intMonth%MAX_MONTH;
				valid = false;
			}
			if(MIN_MONTH-1 > intMonth)
			{
				year += (0 == intMonth/MAX_MONTH ? -1 : intMonth/MAX_MONTH-1);
				intMonth = (0 == intMonth%MAX_MONTH ? 1 : MAX_MONTH + intMonth%MAX_MONTH);
				valid = false;
			}
			if(0 == intMonth)
			{
				year--;
				intMonth = MAX_MONTH;
				valid = false;
			}

			if(numberDays[intMonth-1] < day)
			{
				int oldMonth = intMonth;
				intMonth++;
				day-=numberDays[oldMonth-1];
				valid = false;
			}
			if(MIN_DAY-1 > day)
			{
				int oldMonth = intMonth;
				intMonth--;
				day+=numberDays[oldMonth-1];
				valid = false;
			}
			if(0 == day)
			{
				int oldMonth = intMonth;
				intMonth--;
				day = numberDays[oldMonth-1];
				valid = false;
			}

			
		}
		
	}

}